package com.hy.lightning.boot.web.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import com.hy.lightning.boot.core.ContentType;
import com.hy.lightning.boot.web.servlet.bean.DefaultHttpServletRequest;
import com.hy.lightning.boot.web.servlet.bean.DefaultHttpServletResponse;
import com.hy.lightning.commons.utils.JSON;

public class DefaultFilterHandler implements FilterHandler {

	private Logger logger = Logger.getLogger(getClass());
	
	@Override
	public boolean filter(DefaultHttpServletRequest defaultHttpServletRequest, DefaultHttpServletResponse defaultHttpServletReqsponse) {
		try {
			setCharset(defaultHttpServletRequest, defaultHttpServletReqsponse, "UTF-8");
			setCrossDomain(defaultHttpServletRequest, defaultHttpServletReqsponse);
			showRequestLog(defaultHttpServletRequest, defaultHttpServletReqsponse);
			filterStaticFile(defaultHttpServletRequest, defaultHttpServletReqsponse);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public byte[] responseFilter(DefaultHttpServletResponse response, Object obj) throws JsonGenerationException, JsonMappingException, IOException {
		String contentType = response.getContentType();
		if(contentType.contains(ContentType.JSON)) {
			return JSON.toString(obj).getBytes();
		} else if(contentType.contains(ContentType.OCTET_STREAM)) {
			return (byte[]) obj;
		} else if(contentType.contains(ContentType.IMAGE_JPG)) {
			return (byte[]) obj;
		} else {
			return obj.toString().getBytes();
		} 
	}
	
	/**
	 * 设置编码
	 * @param charset
	 * @throws UnsupportedEncodingException 
	 */
	protected void setCharset(DefaultHttpServletRequest defaultHttpServletRequest, DefaultHttpServletResponse defaultHttpServletReqsponse, String charset) throws UnsupportedEncodingException {
		defaultHttpServletReqsponse.setCharacterEncoding(charset);
		defaultHttpServletRequest.setCharacterEncoding(charset);
	}
	
	/**
	 * 设置跨域
	 * @param defaultHttpServletRequest
	 * @param defaultHttpServletReqsponse
	 */
	protected void setCrossDomain(DefaultHttpServletRequest defaultHttpServletRequest, DefaultHttpServletResponse defaultHttpServletReqsponse) {
		defaultHttpServletReqsponse.setHeader("Access-Control-Allow-Origin", defaultHttpServletRequest.getHeader("origin"));
		defaultHttpServletReqsponse.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
		defaultHttpServletReqsponse.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
		defaultHttpServletReqsponse.setHeader("Access-Control-Allow-Credentials", "true");
	}
	
	/**
	 * 静态文件过滤
	 * @param servletPath
	 * @return
	 * @throws Exception 
	 */
	protected void filterStaticFile(DefaultHttpServletRequest defaultHttpServletRequest, DefaultHttpServletResponse defaultHttpServletReqsponse) throws Exception {
		String servletPath = defaultHttpServletRequest.getServletPath();
		if(servletPath != null && servletPath.contains(".")) throw new Exception("过滤静态文件");
	}
	
	/**
	 * 显示请求日志
	 */
	protected void showRequestLog(DefaultHttpServletRequest defaultHttpServletRequest, DefaultHttpServletResponse defaultHttpServletReqsponse) {
		String servletPath = defaultHttpServletRequest.getServletPath();
		Map<String,String[]> requestBody = defaultHttpServletRequest.getParameterMap();
		try {
			logger.info(String.format("servletPath:%s, requestBody:%s", servletPath, JSON.toString(requestBody)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
